# Coppock, Alexander, Alan S. Gerber, Donald P. Green, and Holger L. Kern. 
# Combining Double Sampling and Bounds to Address Non-Ignorable Missing Outcomes in Randomized Experiments. 
# Political Analysis (in press)
# Appendix Tables 1 and 2

rm(list=ls())

# Set your working directory to the replication archive
# setwd() 

levendusky_mturk_clean <- read.csv("levendusky_mturk_clean.csv")

library(dplyr)
library(xtable)
library(stargazer)

# Uncomment to install latest versions
# install.packages("devtools")
# devtools::install_github("acoppock/attrition")
# devtools::install_github("acoppock/commarobust")

# OR

# Uncomment to install source versions
# install.packages("attrition_0.0.0.9000.tar.gz", repos = NULL, type="source")
# install.packages("commarobust_0.1.0.tar.gz", repos = NULL, type="source")

library(commarobust)
library(attrition)

# Appendix Table 1 ---------------------------------------------------

fit_1 <- lm(L_dif ~ Z_lev, data = filter(levendusky_mturk_clean, R1 == 1))
fit_2 <- lm(L_dif_w2 ~ Z_lev, data = filter(levendusky_mturk_clean, R1 == 1))

fit_3 <- lm(L_ex ~ Z_lev, data = filter(levendusky_mturk_clean, R1 == 1))
fit_4 <- lm(L_ex_w2 ~ Z_lev, data = filter(levendusky_mturk_clean, R1 == 1))

stargazer(fit_1, fit_2, fit_3, fit_4,
          se=makerobustseslist(fit_1, fit_2, fit_3, fit_4),
          p=makerobustpslist(fit_1, fit_2, fit_3, fit_4),
          style = "apsr", title = "Immediate and Over Time Effects of Treatment",
          covariate.labels = c("Treatment: Placebo", "Treatment: Polarized", "Constant"),
          column.labels = c("Perceived Polarization", "Extremity"),column.separate = c(2,2),
          dep.var.labels.include = FALSE, 
          model.numbers = FALSE,
          omit.stat = c("f", "ser", "adj.rsq"),
          add.lines = list(c("Wave", rep(c("Wave 1", "Wave 2"), 2))),
          notes = c("All models are estimated among those who respond in the initial Wave 2 sample.",
                    "Robust standard errors are in parentheses."),column.sep.width = "0pt", 
          out = "ignorable_missingness_mturk.tex")


# Appendix Table 2 ----------------------------------------------------

cis1 <- estimator_ds(Y=L_dif_w2, Z = Z1, R1 = R1, Attempt = Attempt, 
                     R2 = R2, minY = 0, maxY = 6, alpha = 0.05, 
                     data = subset(levendusky_mturk_clean, !is.na(Z1)))
cis2 <- estimator_ds(Y=L_ex_w2,Z = Z1, R1 = R1, Attempt = Attempt, 
                     R2 = R2, minY = 0, maxY = 6, alpha = 0.05, 
                     data = subset(levendusky_mturk_clean, !is.na(Z1)))
cis3 <- estimator_ds(Y=L_dif_w2, Z = Z2, R1 = R1, Attempt = Attempt, 
                     R2 = R2, minY = 0, maxY = 6, alpha = 0.05, 
                     data = subset(levendusky_mturk_clean, !is.na(Z2)))
cis4 <- estimator_ds(Y=L_ex_w2, Z = Z2, R1 = R1, Attempt = Attempt, 
                     R2 = R2, minY = 0, maxY = 6, alpha = 0.05, 
                     data = subset(levendusky_mturk_clean, !is.na(Z2)))
cis5 <- estimator_ds(Y=L_dif_w2, Z = Z3, R1 = R1, Attempt = Attempt, 
                     R2 = R2, minY = 0, maxY = 6, alpha = 0.05, 
                     data = subset(levendusky_mturk_clean, !is.na(Z3)))
cis6 <- estimator_ds(Y=L_ex_w2, Z = Z3, R1 = R1, Attempt = Attempt, 
                     R2 = R2, minY = 0, maxY = 6, alpha = 0.05, 
                     data = subset(levendusky_mturk_clean, !is.na(Z3)))

appendix_table_2 <- 
  cbind(cis1, cis2, cis3, cis4, cis5, cis6)

row.names(appendix_table_2) <- c("95% CI Lower Bound","95% CI Upper Bound", 
                              "Worst-Case Bound: Low Estimate", "Worst-Case Bound: High Estimate",
                              "Variance of Low Estimate", "Variance of High Estimate")

appendix_table_2